package com.tyk.bookstore.back.storage.service.impl;


import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.common.dal.mapper.dto.SinDto;
import com.tyk.bookstore.back.storage.model.dto.Warehouse4WaybillDto;
import com.tyk.bookstore.back.storage.model.dto.WarehouseNameDto;
import com.tyk.bookstore.back.storage.model.entity.WarehouseDo;
import com.tyk.bookstore.back.storage.model.result.WarehouseResultEnum;
import com.tyk.bookstore.back.storage.repository.WarehouseRepository;
import com.tyk.bookstore.back.storage.service.WarehouseApiService;
import com.tyk.bookstore.back.storage.util.MapStructStorage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 仓库管理对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class WarehouseApiServiceImpl implements WarehouseApiService {
    private final WarehouseRepository warehouseRepository;


    @Override
    public String getNameById(Long id) {
        SinDto<String> dto = warehouseRepository.selectSin1Eq(
                WarehouseDo::getName,
                WarehouseDo::getId, id,
                WarehouseDo.class
        );
        AssertUtil.notNull(dto, WarehouseResultEnum.WAREHOUSE_NOT_FOUND);
        return dto.getV1();
    }


    @Override
    public List<WarehouseNameDto> getWarehouseNameDtoListByIdSet(Set<Long> idSet) {
        return warehouseRepository
                .<BigInteger, String, Long>selectBinList1In(
                        WarehouseDo::getId, WarehouseDo::getName,
                        WarehouseDo::getId, idSet,
                        WarehouseDo.class
                )
                .stream().map(it -> new WarehouseNameDto(it.getV1().longValue(), it.getV2())).toList();
    }

    @Override
    public Warehouse4WaybillDto getWarehouse4WaybillDtoById(Long id) {
        Warehouse4WaybillDto dto = warehouseRepository.selectWarehouse4WaybillDtoById(id);
        AssertUtil.notNull(dto, WarehouseResultEnum.WAREHOUSE_NOT_FOUND);
        return dto;
    }
}
